<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>pbliteserializer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_proto2_pbliteserializer.js.html">pbliteserializer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Protocol Buffer 2 Serializer which serializes messages
<a name="line17"></a> *  into PB-Lite (&quot;JsPbLite&quot;) format.
<a name="line18"></a> *
<a name="line19"></a> * PB-Lite format is an array where each index corresponds to the associated tag
<a name="line20"></a> * number. For example, a message like so:
<a name="line21"></a> *
<a name="line22"></a> * message Foo {
<a name="line23"></a> *   optional int bar = 1;
<a name="line24"></a> *   optional int baz = 2;
<a name="line25"></a> *   optional int bop = 4;
<a name="line26"></a> * }
<a name="line27"></a> *
<a name="line28"></a> * would be represented as such:
<a name="line29"></a> *
<a name="line30"></a> * [, (bar data), (baz data), (nothing), (bop data)]
<a name="line31"></a> *
<a name="line32"></a> * Note that since the array index is used to represent the tag number, sparsely
<a name="line33"></a> * populated messages with tag numbers that are not continuous (and/or are very
<a name="line34"></a> * large) will have many (empty) spots and thus, are inefficient.
<a name="line35"></a> *
<a name="line36"></a> *
<a name="line37"></a> */
<a name="line38"></a>
<a name="line39"></a>goog.provide(&#39;goog.proto2.PbLiteSerializer&#39;);
<a name="line40"></a>
<a name="line41"></a>goog.require(&#39;goog.proto2.LazyDeserializer&#39;);
<a name="line42"></a>goog.require(&#39;goog.proto2.Util&#39;);
<a name="line43"></a>
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>/**
<a name="line47"></a> * PB-Lite serializer.
<a name="line48"></a> *
<a name="line49"></a> * @constructor
<a name="line50"></a> * @extends {goog.proto2.LazyDeserializer}
<a name="line51"></a> */
<a name="line52"></a>goog.proto2.PbLiteSerializer = function() {};
<a name="line53"></a>goog.inherits(goog.proto2.PbLiteSerializer, goog.proto2.LazyDeserializer);
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>/**
<a name="line57"></a> * If true, fields will be serialized with 0-indexed tags (i.e., the proto
<a name="line58"></a> * field with tag id 1 will have index 0 in the array).
<a name="line59"></a> * @type {boolean}
<a name="line60"></a> * @private
<a name="line61"></a> */
<a name="line62"></a>goog.proto2.PbLiteSerializer.prototype.zeroIndexing_ = false;
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/**
<a name="line66"></a> * By default, the proto tag with id 1 will have index 1 in the serialized
<a name="line67"></a> * array.
<a name="line68"></a> *
<a name="line69"></a> * If the serializer is set to use zero-indexing, the tag with id 1 will have
<a name="line70"></a> * index 0.
<a name="line71"></a> *
<a name="line72"></a> * @param {boolean} zeroIndexing Whether this serializer should deal with
<a name="line73"></a> *     0-indexed protos.
<a name="line74"></a> */
<a name="line75"></a>goog.proto2.PbLiteSerializer.prototype.setZeroIndexed = function(zeroIndexing) {
<a name="line76"></a>  this.zeroIndexing_ = zeroIndexing;
<a name="line77"></a>};
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>/**
<a name="line81"></a> * Serializes a message to a PB-Lite object.
<a name="line82"></a> *
<a name="line83"></a> * @param {goog.proto2.Message} message The message to be serialized.
<a name="line84"></a> * @return {!Array} The serialized form of the message.
<a name="line85"></a> * @override
<a name="line86"></a> */
<a name="line87"></a>goog.proto2.PbLiteSerializer.prototype.serialize = function(message) {
<a name="line88"></a>  var descriptor = message.getDescriptor();
<a name="line89"></a>  var fields = descriptor.getFields();
<a name="line90"></a>
<a name="line91"></a>  var serialized = [];
<a name="line92"></a>  var zeroIndexing = this.zeroIndexing_;
<a name="line93"></a>
<a name="line94"></a>  // Add the known fields.
<a name="line95"></a>  for (var i = 0; i &lt; fields.length; i++) {
<a name="line96"></a>    var field = fields[i];
<a name="line97"></a>
<a name="line98"></a>    if (!message.has(field)) {
<a name="line99"></a>      continue;
<a name="line100"></a>    }
<a name="line101"></a>
<a name="line102"></a>    var tag = field.getTag();
<a name="line103"></a>    var index = zeroIndexing ? tag - 1 : tag;
<a name="line104"></a>
<a name="line105"></a>    if (field.isRepeated()) {
<a name="line106"></a>      serialized[index] = [];
<a name="line107"></a>
<a name="line108"></a>      for (var j = 0; j &lt; message.countOf(field); j++) {
<a name="line109"></a>        serialized[index][j] =
<a name="line110"></a>            this.getSerializedValue(field, message.get(field, j));
<a name="line111"></a>      }
<a name="line112"></a>    } else {
<a name="line113"></a>      serialized[index] = this.getSerializedValue(field, message.get(field));
<a name="line114"></a>    }
<a name="line115"></a>  }
<a name="line116"></a>
<a name="line117"></a>  // Add any unknown fields.
<a name="line118"></a>  message.forEachUnknown(function(tag, value) {
<a name="line119"></a>    var index = zeroIndexing ? tag - 1 : tag;
<a name="line120"></a>    serialized[index] = value;
<a name="line121"></a>  });
<a name="line122"></a>
<a name="line123"></a>  return serialized;
<a name="line124"></a>};
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/** @override */
<a name="line128"></a>goog.proto2.PbLiteSerializer.prototype.deserializeField =
<a name="line129"></a>    function(message, field, value) {
<a name="line130"></a>
<a name="line131"></a>  if (value == null) {
<a name="line132"></a>    // Since value double-equals null, it may be either null or undefined.
<a name="line133"></a>    // Ensure we return the same one, since they have different meanings.
<a name="line134"></a>    return value;
<a name="line135"></a>  }
<a name="line136"></a>
<a name="line137"></a>  if (field.isRepeated()) {
<a name="line138"></a>    var data = [];
<a name="line139"></a>
<a name="line140"></a>    goog.proto2.Util.assert(goog.isArray(value));
<a name="line141"></a>
<a name="line142"></a>    for (var i = 0; i &lt; value.length; i++) {
<a name="line143"></a>      data[i] = this.getDeserializedValue(field, value[i]);
<a name="line144"></a>    }
<a name="line145"></a>
<a name="line146"></a>    return data;
<a name="line147"></a>  } else {
<a name="line148"></a>    return this.getDeserializedValue(field, value);
<a name="line149"></a>  }
<a name="line150"></a>};
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/** @override */
<a name="line154"></a>goog.proto2.PbLiteSerializer.prototype.getSerializedValue =
<a name="line155"></a>    function(field, value) {
<a name="line156"></a>  if (field.getFieldType() == goog.proto2.FieldDescriptor.FieldType.BOOL) {
<a name="line157"></a>    // Booleans are serialized in numeric form.
<a name="line158"></a>    return value ? 1 : 0;
<a name="line159"></a>  }
<a name="line160"></a>
<a name="line161"></a>  return goog.proto2.Serializer.prototype.getSerializedValue.apply(this,
<a name="line162"></a>                                                                   arguments);
<a name="line163"></a>};
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/** @override */
<a name="line167"></a>goog.proto2.PbLiteSerializer.prototype.getDeserializedValue =
<a name="line168"></a>    function(field, value) {
<a name="line169"></a>
<a name="line170"></a>  if (field.getFieldType() == goog.proto2.FieldDescriptor.FieldType.BOOL) {
<a name="line171"></a>    // Booleans are serialized in numeric form.
<a name="line172"></a>    return value === 1;
<a name="line173"></a>  }
<a name="line174"></a>
<a name="line175"></a>  return goog.proto2.Serializer.prototype.getDeserializedValue.apply(this,
<a name="line176"></a>                                                                     arguments);
<a name="line177"></a>};
<a name="line178"></a>
<a name="line179"></a>
<a name="line180"></a>/** @override */
<a name="line181"></a>goog.proto2.PbLiteSerializer.prototype.deserialize =
<a name="line182"></a>    function(descriptor, data) {
<a name="line183"></a>  var toConvert = data;
<a name="line184"></a>  if (this.zeroIndexing_) {
<a name="line185"></a>    // Make the data align with tag-IDs (1-indexed) by shifting everything
<a name="line186"></a>    // up one.
<a name="line187"></a>    toConvert = [];
<a name="line188"></a>    for (var key in data) {
<a name="line189"></a>      toConvert[parseInt(key, 10) + 1] = data[key];
<a name="line190"></a>    }
<a name="line191"></a>  }
<a name="line192"></a>  return goog.base(this, &#39;deserialize&#39;, descriptor, toConvert);
<a name="line193"></a>};
</pre>


</body>
</html>
